package com.project.imageinterference.controller;

import com.project.imageinterference.dto.request.DiseaseIdentifyPageReq;
import com.project.imageinterference.dto.request.DiseaseIdentifyReq;
import com.project.imageinterference.dto.request.DiseaseIdentifyUpdateReq;
import com.project.imageinterference.dto.response.DiseaseIdentifyPageRes;
import com.project.imageinterference.dto.response.DiseaseIdentifyRes;
import com.project.imageinterference.entity.SysUser;
import com.project.imageinterference.exception.BusinessException;
import com.project.imageinterference.service.DiseaseIdentifyService;
import com.project.imageinterference.vo.Paging;
import com.project.imageinterference.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 疾病识别表 前端控制器
 * </p>
 *
 * @author haochuanwan
 * @since 2025-02-22
 */
@Api(tags = "疾病识别接口")
@RestController
@RequestMapping("/diseaseIdentify")
public class DiseaseIdentifyController {

    @Autowired
    private DiseaseIdentifyService diseaseIdentifyService;

    @ApiOperation(value = "保存识别结果")
    @PostMapping("/save")
    public Result saveDiseaseIdentify(@RequestBody DiseaseIdentifyReq diseaseIdentifyReq, HttpServletRequest httpServletRequest) {
        try {
//            String token = httpServletRequest.getHeader("token");
            SysUser sysUserPo = null;
            sysUserPo = new SysUser();
            sysUserPo.setId(String.valueOf(1));
            sysUserPo.setUserName("admin");
            sysUserPo.setName("admin");
//            if(!StringUtils.isEmpty(token)) {
                //TODO 先写死用户信息，后面集成 jwt 后再放开
//                String userinfo = TokenUtils.getUserInfoFromToken(token);
//                sysUserPo = JSONUtil.toBean(userinfo, SysUser.class);
//            }
            diseaseIdentifyService.saveDiseaseIdentify(diseaseIdentifyReq, sysUserPo);
            return Result.success("保存成功");
        }catch (BusinessException e) {
            return Result.failure(e.getMessage());
        } catch (Exception e) {
            return Result.failure("保存失败: " + e.getMessage());
        }
    }

    @ApiOperation(value = "查询识别结果详情")
    @GetMapping(value = "info")
    public Result<DiseaseIdentifyRes> getDiseaseIdentifyInfo(@RequestParam("id") Integer id){
        try {
            DiseaseIdentifyRes diseaseIdentifyInfo = diseaseIdentifyService.getDiseaseIdentifyInfo(id);
            return Result.success(diseaseIdentifyInfo);
        }catch (BusinessException e) {
            return Result.failure(e.getMessage());
        } catch (Exception e) {
            return Result.failure("查询失败: " + e.getMessage());
        }
    }

    @ApiOperation(value = "分页查询识别结果列表")
    @PostMapping(value = "page")
    public Result<Paging<DiseaseIdentifyPageRes>> queryDiseaseIdentifyPage(@RequestBody DiseaseIdentifyPageReq req){
        try {
            Paging<DiseaseIdentifyPageRes> page = diseaseIdentifyService.queryDiseaseIdentifyPage(req);
            return Result.success(page);
        }catch (BusinessException e) {
            return Result.failure(e.getMessage());
        } catch (Exception e) {
            return Result.failure("查询失败: " + e.getMessage());
        }
    }

    @ApiOperation(value = "更新识别结果")
    @PutMapping(value = "update")
    public Result updateDiseaseIdentify(@RequestBody DiseaseIdentifyUpdateReq diseaseIdentifyUpdateReq){
        try {
            diseaseIdentifyService.updateDiseaseIdentify(diseaseIdentifyUpdateReq);
            return Result.success("更新成功");
        }catch (BusinessException e) {
            return Result.failure(e.getMessage());
        } catch (Exception e) {
            return Result.failure("查询失败: " + e.getMessage());
        }
    }

    @ApiOperation(value = "删除识别结果")
    @DeleteMapping(value = "delete")
    public Result deleteDiseaseIdentify(@RequestParam("id") Integer id){
        try {
            diseaseIdentifyService.deleteDiseaseIdentify(id);
            return Result.success("删除成功");
        }catch (BusinessException e) {
            return Result.failure(e.getMessage());
        } catch (Exception e) {
            return Result.failure("删除失败: " + e.getMessage());
        }
    }
}
